package com.hicorp.segment.controller;

import com.hicorp.segment.pojo.po.MaterialConcrete;
import com.hicorp.segment.pojo.po.Receipts;
import com.hicorp.segment.service.ReceiptsService;
import com.hicorp.segment.utils.ResultBean;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/receipts/{tableName}")
public class ReceiptsController {
    private final ReceiptsService receiptsService;

    public ReceiptsController(ReceiptsService receiptsService) {
        this.receiptsService = receiptsService;
    }

    // 查询
    @GetMapping
    public ResultBean<List<Receipts>> selectAll(@PathVariable String tableName) {
        return receiptsService.queryInfo(tableName);
    }

    // 新增
    @PostMapping("/{authorityCode}")
    public ResultBean<Long> createRecord(@PathVariable String tableName, @PathVariable String authorityCode,@RequestBody Receipts receipts) {
      //  receipts.setTableName(tableName);
        return receiptsService.createRecord(tableName,receipts,authorityCode);
    }

    // 修改
    @PutMapping("/{authorityCode}/{id}")
    public ResultBean<Long> updateRecord(@PathVariable String tableName, @PathVariable Long id, @RequestBody Receipts receipts) {

        return receiptsService.updateRecord(tableName,receipts);
    }

    // 批量删除
    @PostMapping("/batch-delete")
    public Object batchDelete(@PathVariable String tableName, @RequestBody List<Long> ids) {
        return receiptsService.batchDeleteRecord(tableName, ids);
    }

    // 批量作废
    @PostMapping("/batchInvalid")
    public Object batchInvalid(@PathVariable String tableName, @RequestBody List<Long> ids) {
        return receiptsService.batchInvalidRecord(tableName, ids);
    }

    // 组合搜索
    @PostMapping ("/search/{queryCode}")
    public Object searchRecord(@PathVariable String tableName,@PathVariable String queryCode, @RequestBody Receipts receipts) {
       // receipts.setTableName(tableName);
        System.out.println(queryCode);
        return receiptsService.searchRecord(tableName,receipts,queryCode);
    }

    // 生成bom清单
    @PostMapping("/search/createBoom/{orderId}")
    public Object boomCreate(@PathVariable String tableName,@PathVariable String orderId) {
        return receiptsService.boomCreate(orderId);
    }
    // 搜索
    @GetMapping("/{field}/{content}")
    public Object selectRecord(@PathVariable String tableName, @PathVariable String field, @PathVariable String content) {
        return receiptsService.selectRecords(tableName, field, content);
    }

    // 生成单据内部编码
    @GetMapping("/generateReceiptsNo/{preNo}")
    public ResultBean<String> generateReceiptsNo(@PathVariable String tableName,@PathVariable String preNo) {
        return receiptsService.generateReceiptsNo(tableName, preNo);
    }

    // 查询，通过表名和内部编码来获取主子表数据
    @GetMapping("/getReceiptByNo/{receiptNo}")
    public ResultBean<Receipts> selectByTableNameAndNo(@PathVariable String tableName, @PathVariable String receiptNo) {
        return receiptsService.selectByTableNameAndNo(tableName,receiptNo);
    }

    // 远程搜索数据获取，通过表名
    @GetMapping("/autocompleteInput/material")
    public ResultBean<List<MaterialConcrete>> selectAutocompleteInput(@PathVariable String tableName) {
        return receiptsService.selectAutocompleteInput(tableName);
    }
}
